#include "dev.h"
#include "def.h"
#include "sys.h"

#if 0

void USART3_SendByte(u8 Data)
{
	USART_SendData(USART3,Data);
	while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}

void USART3_SendString(u8 *Data,u16 len)
{
	for(int i=0;i<len;i++)
	{
		USART3_SendByte(Data[i]);
		while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
	}
}

void USART3_DMA_SendString(u8 *str,u16 len)	
{ 
	u16 i;
	if(len > 0)
	{			
		for(i=0;i<len;i++)
		{
			USART3_DMA_TXBuf[i]=str[i];
		}
		DMA_Cmd(DMA1_Channel2, DISABLE);
		DMA1_Channel2->CNDTR=len;
		DMA_Cmd(DMA1_Channel2, ENABLE);			
		printf("snd call....................\r\n");
	}
}	


//在未进入透传模式的情况下通过usart3中断获取8266自动重连信息
void ESP_Chk(void)
{
	if(!gSys.conn.ap_link)
	{
		if(strstr((char*)USART3_DMA_RXBuf,"WIFI CONNECTED"))
		{
			gSys.conn.ap_link = 1;
			printf("irq----->ap link chk!\r\n");
		}
	}
	else
	{
		if(strstr((char*)USART3_DMA_RXBuf,"WIFI GOT IP"))
		{
			gSys.conn.ip_link = 1;
			printf("irq----->ip link chk!\r\n");
		}
	}
	
	if(strstr((char*)USART3_DMA_RXBuf,"+CIPSTA_CUR") && strstr((char*)USART3_DMA_RXBuf,"OK"))
	{
		int ip[4]={0};		
		sscanf((char*)USART3_DMA_RXBuf,"%*[^:]:ip:\"%d.%d.%d.%d\"[^\"]",&ip[0],&ip[1],&ip[2],&ip[3]);
		for(int i=0;i<4;i++)
		{
			devIP[i]=ip[i];
		}
	}
	
	if(strstr((char*)USART3_DMA_RXBuf,"+CIPSTAMAC") && strstr((char*)USART3_DMA_RXBuf,"OK"))
	{
		printf("irq----->mac chk!");
		gSys.conn.get_mac=1;
		//读取两个引号之间数据格式化处理
		int mac[6]={0};
		
		//直接写devMAC有错误
		sscanf((char*)USART3_DMA_RXBuf,"%*[^\"]\"%x:%x:%x:%x:%x:%x[^\"]",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
		
		for(int i=0;i<6;i++)
		{
			devMAC[i]=mac[i];
		}
		printf("# %x:%x:%x:%x:%x:%x\r\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
	}
}


void USART3_IRQHandler(void)	
{
	u16 i;
 	if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
	{	
		DMA_Cmd(DMA1_Channel3, DISABLE);//??DMA,?????????
		USART3_Recv_Len=USART3_RX_MAX-DMA_GetCurrDataCounter(DMA1_Channel3);
		USART3_DMA_RXBuf[USART3_Recv_Len]='\0';
		printf("\r\n-----------------------------------usart3 rcv[%d]:\r\n%s\r\n-----------------------------------\r\n",USART3_Recv_Len,USART3_DMA_RXBuf);
		if(gSys.conn.start_xmit)
		{
			//透传判断
			//osMessageQueuePut(mqttRcvQueueID,&USART3_DMA_RXBuf,NULL,0);
			service_process();
		}
		else
		{
			//未透传判断
			ESP_Chk();
		}
		//*************************************//
		DMA_ClearFlag(DMA1_FLAG_GL3 | DMA1_FLAG_TC3 | DMA1_FLAG_TE3 | DMA1_FLAG_HT3);//???
		DMA1_Channel3->CNDTR = USART3_RX_MAX;//???
		DMA_Cmd(DMA1_Channel3, ENABLE);//???,??DMA
		//?SR??DR??Idle
		i = USART3->SR;
		i = USART3->DR;
	}
	if(USART_GetITStatus(USART3, USART_IT_PE | USART_IT_FE | USART_IT_NE) != RESET)//??
	{
		printf("dma err 33!!!!!!!!!!!!!!!!!!\r\n");
		USART_ClearITPendingBit(USART3, USART_IT_PE | USART_IT_FE | USART_IT_NE);
	}
	USART_ClearITPendingBit(USART3, USART_IT_IDLE);
}


void DMA1_Channel2_IRQHandler(void)
{	 
	DMA_ClearITPendingBit(DMA1_IT_TC2);
	DMA_ClearITPendingBit(DMA1_IT_TE2);
	DMA_Cmd(DMA1_Channel2, DISABLE);//??DMA
}

void DMA1_Channel3_IRQHandler(void)
{
	DMA_ClearITPendingBit(DMA1_IT_TC3);
	DMA_ClearITPendingBit(DMA1_IT_TE3);
	DMA_Cmd(DMA1_Channel3, DISABLE);//??DMA,?????????
	DMA1_Channel3->CNDTR = USART3_RX_MAX;//???
	DMA_Cmd(DMA1_Channel3, ENABLE);//???,??DMA
}


void USART3_Init(u32 bound)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	USART_InitTypeDef	USART_InitStructure;
	NVIC_InitTypeDef	NVIC_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA1
	
	USART_DeInit(USART3);
	
	//USART3_TX PB.10
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	//USART3_RX PB.11
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	//USART3 NVIC
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	USART_Init(USART3, &USART_InitStructure);
	

	USART_ITConfig(USART3,USART_IT_TC,DISABLE);  //关闭发送中断 
	USART_ITConfig(USART3,USART_IT_RXNE,DISABLE);//关闭接收中断  
	USART_ITConfig(USART3,USART_IT_IDLE,ENABLE); //开启总线空闲中断 
	USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
	USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
	USART_Cmd(USART3, ENABLE);
	
	//DMA1 Channel2(TX)Config
	DMA_DeInit(DMA1_Channel2);	
	DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&USART3->DR;
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART3_DMA_TXBuf;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStructure.DMA_BufferSize = USART3_TX_MAX;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA????DMA_Mode_Circular,DMA_Mode_Normal
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel2, &DMA_InitStructure);
	DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
	DMA_ITConfig(DMA1_Channel2, DMA_IT_TE, ENABLE);
	
	//Enable USART3 DMA TX request
	DMA_Cmd(DMA1_Channel2, DISABLE);//??

	//DMA1 Channel3(RX)Config
	DMA_DeInit(DMA1_Channel3);	
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART3_DMA_RXBuf;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_BufferSize = USART3_RX_MAX;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA????DMA_Mode_Circular,DMA_Mode_Normal
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel3, &DMA_InitStructure);
	DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);
	DMA_ITConfig(DMA1_Channel3, DMA_IT_TE, ENABLE);
	
	//Enable USART3 DMA RX request
	DMA_Cmd(DMA1_Channel3, ENABLE);//??
}
#endif
